﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CardControlLibrary
{
    public partial class CardGroupView : UserControl
    {

        private CardGroupModel mCardGroupModel;
        private CardThreadSafeOpperations m_UI_TS; 

        public CardGroupView(CardGroupModel cgm, Control parent)
        {
            InitializeComponent();
            mCardGroupModel = cgm;
            m_Collapsed = false;
            m_GroupHeight = MinHeight;
            m_UI_TS = new CardThreadSafeOpperations();

            foreach (CardModel cm in mCardGroupModel.GetCards)
            {
                CardView cv = new CardView(cm);
                cv.OnCollapseExpand += CollapseExpandHandler;
                m_UI_TS.AddControl(parent, cv);
                m_Children.Add(cv);
                m_UI_TS.SetVisibility(cv, false);
            }
        }

        private bool m_Collapsed = false;
        public bool GetCollapsed()
        {
            return m_Collapsed;
        }
        public delegate void CollapseExpandGroupHandler(CardGroupView cc);
        public event CollapseExpandGroupHandler OnCollapseExpand;
        protected void OnCollapseExpandHandler(CardGroupView cc)
        {
            if (OnCollapseExpand != null)
            {
                OnCollapseExpand(cc);
            }
        }

        public int MinWidth { get { return 220; } }
        public int MinHeight { get { return 25; } }
        public int MaxWidth { get { return 200; } }
        public int MaxHeight { get { return 25; } }

        private int m_GroupHeight;
        public int GetGroupHeight { get { return m_GroupHeight; } }

        private List<CardView> m_Children = new List<CardView>();

        public void ClearGroup()
        {
            Control parent = this.Parent;
            foreach (CardView cv in m_Children) 
                m_UI_TS.RemoveControl(parent, cv);
            m_Children.Clear();
            m_GroupHeight = MinHeight; 
        }


        public void DrawControl()
        {
            m_UI_TS.SetWidth(this, MinWidth);
            m_UI_TS.SetHeight(this,MinHeight);
            m_UI_TS.SetWidth(pictureBox1, 20);
            m_UI_TS.SetHeight(pictureBox1, 20);
            m_UI_TS.SetLocation(pictureBox1, new Point(5, 5));
            m_UI_TS.SetLocation(lbGroupName, new Point(30, 8));
            m_UI_TS.SetText(lbGroupName, mCardGroupModel.Title);
            m_GroupHeight = MinHeight;

            if (!m_Collapsed)
            {
                m_UI_TS.SetImage(pictureBox1, Properties.Resources.up_arrow);
                foreach (CardView cv in m_Children)
                {
                    m_UI_TS.SetLocation(cv, new Point(this.Location.X + 20, this.Location.Y + m_GroupHeight)); 
                    cv.DrawControl();
                    m_UI_TS.SetVisibility(cv, true);     
                    m_GroupHeight += cv.Height;
                    
                }
            }
            else
            {
                m_UI_TS.SetImage(pictureBox1, Properties.Resources.down_arrow);
                foreach (CardView cv in m_Children)
                {
                    cv.Hide();
                }
            }


        }

        private void CollapseExpandHandler(CardView cc)
        {
            OnCollapseExpandHandler(this);
            //this.Parent.Invalidate();
        }

/// <summary>
/// /
/// </summary>
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            m_Collapsed = !m_Collapsed;
            //DrawControl();
            OnCollapseExpandHandler(this);
        }
    }
}
